<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends BaseController {
    public function index(){
        $id = $_GET['id'];                  //获取文章的id
        $m=M("text");
        $articleInfo = $m->where("id = {$id}")->find();         //获取文章的信息
        
        if($articleInfo){
            //作者信息
            $mm=M("user");
            $m->where("id = {$id}")->setInc('view',1);
            $userInfo = $mm->where("id = {$articleInfo['bid']}")->find();
            //作者头像
            $picid = $userInfo['id'];
            $x =M("userinfo");
            $userinfo = $x->where("uid = {$picid}")->find();
            $this->assign("userPic",$userinfo['headpic']);  
            //分类信息
            $mmm=M("pathclass");
            $fenleiInfo = $mmm->where("id = {$articleInfo['tid']}")->find(); 
            //该作者文章总数
            $articleCount = $m->where("bid = {$userInfo['id']} AND status = 1")->count();
            //dump($articleCount);
            
            $zuixin = $m->where("bid = {$userInfo['id']} AND status = 1")->limit(5)->order("id desc")->select();     //该作者最新的5篇文章

            $this->assign("zuixin",$zuixin);
            $this->assign("articleCount",$articleCount);
            $this->assign("articleInfo",$articleInfo);
            $this->assign("userInfo",$userInfo);
            $this->assign("fenleiInfo",$fenleiInfo);
    /*public function index(){
        $id = $_GET['id'];
        $m=M("text");
        $articleInfo = $m->where("id = {$id}")->find();
        if($articleInfo){
            $mm=D("user");
            $m->where("id = {$id}")->setInc('view',1);
            $userInfo = $mm->where("id = {$articleInfo['bid']}")->relation(true)->find();
            $mmm=M("pathclass");
            $fenleiInfo = $mmm->where("id = {$articleInfo['tid']}")->find();
            $articleCount = $m->where("id = {$userInfo['id']} AND status = 1")->count();
            $zuixin = $m->where("id = {$userInfo['id']} AND status = 1")->limit(5)->order("id desc")->select();
            $this->assign("zuixin",$zuixin);
            $this->assign("articleCount",$articleCount);
            $this->assign("articleInfo",$articleInfo);
            $this->assign("userInfo",$userInfo);
            $this->assign("fenleiInfo",$fenleiInfo);*/
            //遍历评论

            //$k = D('usercomment');
              $Model = new \Think\Model();

        // $sql = "select u.nickname uname,c.id lid,t.id kid,c.cid cid,c.content content from pro_user as u,pro_text as t,pro_usercomment as c where u.id=c.pid AND c.cid = $id order by c.id asc";
        $sql="select u.nickname uname,c.id lid,c.cid cid,c.content content from pro_usercomment c ,pro_user u where u.id=c.pid and  cid in( select id from pro_text where id=$id)";

        //dump($sql);
            $contlist = $Model->query($sql);
            
            //dump($contlist);
            $this->assign('contlist',$contlist);

            
            $this->display();
        }else{
            $this->show("<center><h1>你迷路了</h1></center>");
        }
        
    }

    public function replay(){
        $tid = $_GET['id'];
        $m =M("usercomment");
        $data = $m->create();
        $data['tid'] = $tid;
        $data['createtime']=time();
        if($_SESSION['fix_user']!=null || $_SESSION['fix_user']!= ''){
            $data['uid'] = $_SESSION['fix_user']['id'];
        }
        //dump($_SESSION);
        $result = $m->add($data);
        if($result>0){
            $mm = M("text");
            /*if(!$this->email_set['comment_set']){
                $m = M('email_type');*/
                $info= $m->find(1);
                $article= $mm->find($tid);
                /*$info['send_comment_content'] = $info['send_comment_content']."<br/>回复帖子标题：<a href = '".$_SERVER['HTTP_REFERER']."' target = '_blank'>{$article['title']}</a><br/>回复署名：{$_POST['name']}<br/>回复内容：{$_POST['content']}";
                $this->MySmtp($this->admin_email,$info['send_comment_title'],$info['send_comment_content']);*/
            //}
            $this->success("回复成功！");
            $a=$mm->where("id = {$tid}")->setInc('comment');
        }else{
            $this->error("回复失败！");
        }
    }

    public function getReplay(){
        $id  = intval($_POST['id']);
        $num = intval($_POST['num']);
        $m = M("usercomment");
        $arr = $m->where("tid = {$id}")->limit($num,10)/*->relation(true)*/->order("id asc")->select();
        if(!empty($arr)){
            $this->ajaxReturn($arr);
        }else{
            echo 5;
            exit;
        }
    }




}
